/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.common.CurrentUser;
import com.yunqian.jwt.JwtUser;
import com.yunqian.redot.constant.BooleanStatus;
import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.constant.TicketType;
import com.yunqian.redot.domain.Ticket;
import com.yunqian.redot.service.OperationLogService;
import com.yunqian.redot.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Describe: 门票管理控制器
 *
 * @author zsy
 * 2018-09-29 15:55:11
 */
 @RestController
 @RequestMapping("/api/ticket")
public class TicketController {

     @Autowired
     private TicketService ticketService;
     @Autowired
     private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Ticket> pagination(@RequestBody Map<String, Object> params) {
        params.put("_filter_eq_status", BooleanStatus.YES.getCode());
        return ticketService.pagination(params);
    }

     /**
     * 获取所有基础票列表
     *
     * @return
     */
    @GetMapping("/base/all")
    public Map<String, Object> list() {
        List<Ticket> ticketList = ticketService.list(TicketType.BASE_TICKET.getCode());
        Map<String, Object> map = new HashMap<>(16);
        map.put("baseTicketList", ticketList);
        return map;
    }

    /**
     * 新增门票
     *
     * @param ticket
     */
    @PostMapping
    public Ticket save(@RequestBody @Valid Ticket ticket, @CurrentUser JwtUser user) {
        return ticketService.save(ticket, user.getId());
    }

    /**
     * 编辑门票
     *
     * @param id
     * @param modifyData
     * @return
     */
    @PutMapping("/{id}")
    public Ticket update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData, @CurrentUser JwtUser user) {
        return ticketService.update(id, modifyData, user.getId());
    }

    /**
     * 门票详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Ticket detail(@PathVariable("id") Long id) {
        return ticketService.findStrictOne(id);
    }

    /**
     * 删除门票 -逻辑删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id, @CurrentUser JwtUser user){
        Map<String, Object> params = new HashMap<>(16);
        params.put("status", BooleanStatus.NO.getCode());
        Ticket ticket = ticketService.update(id, params);
        if(ticket.getType() == 1){
            operationLogService.save(OperationMenus.DEFAULT_TICKET, Operation.UPDATE, user.getId());
        }else {
            operationLogService.save(OperationMenus.PACKAGES_TICKETS, Operation.UPDATE, user.getId());
        }
    }
}
